const car = require('./src/mock/d.json')
const data = require('./src/mock')
const bodyParser = require('body-parser')
module.exports = {
	devServer: {
		before(app) {
			app.use(bodyParser.json())
			app.get('/api/car/list', (req, res) => {
				res.send({
					code: 200,
					data: car
				})
			})

			app.get('/api/list', (req, res) => {
				const {
					pageCode = 1, pageSize = 10
				} = req.query;

				if (pageCode * pageSize <= data.length) {
					res.send({
						code: 200,
						data: data.slice((pageCode - 1) * pageSize, pageCode * pageSize),
						total: data.length,
						message: '数据加载了10条'
					})
				} else {
					res.send({
						code: 201,
						data: null,
						message: '没有更多数据了'
					})
				}

			})


			app.get('/api/select/car/:id', (req, res) => {
				const { id } = req.params
				const result = data.find(v => v.id === id)
				if(result) {
					res.send({
						code: 200,
						data: result
					})
				} else {
					res.send({
						code: 404,
						data: null,
						message: '该商品已经下架！！！'
					})
				}
			
			})
		}
	}
}